package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.Love;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.NewsPageMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.CollecQueryObject;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/19 15:28
 * 总结:
 */
@Service
public class CollectServiceImpl implements ICollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private NewsPageMapper newsPageMapper;

    @Override
    public Integer saveOrUpdate(Collect collect) {
        //设置当前用户id
        User currentUser = UserContext.getCurrentUser();
        collect.setUser(currentUser);
        //查询表中是否已经存在这条数据
        Collect collect1 =  collectMapper.selectCollect(collect);
        if(collect1 == null){
            //设置收藏时间
            collect.setCollectTime(new Date());
            // 新增
            collectMapper.insert(collect);
            return collect.getState();
        } else if(collect1.getState()== 0){//取消收藏状态
            //设置状态
            collect1.setState(1);//设置收藏状态
            //判断是游记、攻略还是日报
            if (collect.getType() == Collect.TYPE_TRAVEL) {
                //判断文章的类型 获取收藏数+1
                int collectNum = travelMapper.selectCollectNum(collect.getCollectId())+1;
                //通过文章的id修改收藏数量
                travelMapper.updateCollectNum(collect.getCollectId(),collectNum);
            } else if (collect.getType() == Collect.TYPE_STRATEGY) {
                int collectNum = strategyMapper.selectCollectNum(collect.getCollectId())+1;
                strategyMapper.updateCollectNum(collect.getCollectId(),collectNum);
            } else {
                int collectNum = newsPageMapper.selectCollectNum(collect.getCollectId())+1;
                newsPageMapper.updateCollectNum(collect.getCollectId(),collectNum);
            }

            collectMapper.updateByPrimaryKey(collect1);
        }else{
            //设置为取消收藏状态
            collect1.setState(0);

            if (collect.getType() == Collect.TYPE_TRAVEL) {
                //判断文章的类型 获取收藏数-1
                int collectNum = travelMapper.selectCollectNum(collect.getCollectId())-1;
                //通过文章的id修改收藏数量
                travelMapper.updateCollectNum(collect.getCollectId(),collectNum);
            } else if (collect.getType() == Collect.TYPE_STRATEGY) {
                int collectNum = strategyMapper.selectCollectNum(collect.getCollectId())-1;
                strategyMapper.updateCollectNum(collect.getCollectId(),collectNum);
            } else {
                int collectNum = newsPageMapper.selectCollectNum(collect.getCollectId())-1;
                newsPageMapper.updateCollectNum(collect.getCollectId(),collectNum);
            }
            // 更新
            collectMapper.updateByPrimaryKey(collect1);
        }
        return collect1.getState();
    }



    @Override
    public Collect selectByPrimaryKey(Long id) {
        return collectMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Collect> selectAll() {
        return collectMapper.selectAll();
    }

    @Override
    public int selectCount(Collect collect) {

        return  collectMapper.selectCount(collect);
    }

    @Override
    public Collect selectByUserId(Long userId,Long collectId) {
        return collectMapper.selectByUserId(userId,collectId);
    }

    @Override
    public Collect selectByState(Collect collect) {
        //设置当前用户id
        User currentUser = UserContext.getCurrentUser();
        collect.setUser(currentUser);
        return collectMapper.selectByState(collect);
    }

    @Override
    public List<Collect> getAllType() {
        return collectMapper.getAllType();
    }



}
